import 'package:country_code_picker/country_code_picker.dart';
import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/splash_controller.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/images.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:efood_multivendor/view/base/custom_text_field.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SignUpScreen extends StatefulWidget {
  final String phone;
  SignUpScreen({this.phone = ''});
  @override
  _SignUpScreenState createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State<SignUpScreen> {
  final FocusNode _firstNameFocus = FocusNode();
  final FocusNode _referralFocus = FocusNode();
  final TextEditingController _firstNameController = TextEditingController();
  final TextEditingController _referralController = TextEditingController();
  String? _countryDialCode;

  @override
  void initState() {
    super.initState();

    _countryDialCode = CountryCode.fromCountryCode(
            Get.find<SplashController>().configModel!.country!)
        .dialCode;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: Stack(children: [
        Image.asset(
          Images.setLocation,
          height: 100.h,
          width: 100.w,
          fit: BoxFit.fitHeight,
        ),
        Scrollbar(
          child: SingleChildScrollView(
            padding: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
            physics: BouncingScrollPhysics(),
            child: Center(
              child: Container(
                width: context.width > 700 ? 700 : context.width,
                padding: context.width > 700
                    ? EdgeInsets.all(Dimensions.PADDING_SIZE_DEFAULT)
                    : null,
                decoration: context.width > 700
                    ? BoxDecoration(
                        color: Theme.of(context).cardColor,
                        borderRadius:
                            BorderRadius.circular(Dimensions.RADIUS_SMALL),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.grey[Get.isDarkMode ? 700 : 300]!,
                              blurRadius: 5,
                              spreadRadius: 1)
                        ],
                      )
                    : null,
                child: GetBuilder<AuthController>(builder: (authController) {
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(height: 10.h),
                              Text(
                                'fill_in_info'.tr,
                                textAlign: TextAlign.start,
                                style: sfMedium.copyWith(
                                    fontWeight: FontWeight.bold,
                                    fontSize:
                                        Dimensions.fontSizeExtraLarge * 1.8),
                              ),
                              SizedBox(
                                height: 2.h,
                              ),
                              SizedBox(
                                width: 80.h,
                                child: Text(
                                  'fill_in_info_desc'.tr,
                                  textAlign: TextAlign.start,
                                  style: sfRegular.copyWith(
                                      fontSize: Dimensions.fontSizeSmall * 1.1,
                                      color: Theme.of(context).disabledColor),
                                ),
                              ),
                              SizedBox(height: 2.h),
                              CustomTextField(
                                hintText: 'name'.tr,
                                controller: _firstNameController,
                                focusNode: _firstNameFocus,
                                inputType: TextInputType.name,
                                capitalization: TextCapitalization.words,
                                divider: true,
                              ),
                              SizedBox(height: 1.h),
                              CustomTextField(
                                hintText: 'referral_textbox'.tr,
                                controller: _referralController,
                                focusNode: _referralFocus,
                                inputType: TextInputType.name,
                                capitalization: TextCapitalization.words,
                                divider: true,
                              ),
                            ],
                          ),
                          SizedBox(height: 40.h),
                          GestureDetector(
                            onTap: () async {
                              _register(authController);
                            },
                            child: Container(
                              height: 7.h,
                              width: 90.w,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(1.5.h),
                                  gradient: LinearGradient(
                                      begin: Alignment.topLeft,
                                      end: Alignment.bottomRight,
                                      colors: [
                                        Theme.of(context).primaryColor,
                                        Theme.of(context).colorScheme.secondary
                                      ])),
                              child: Center(
                                child: !authController.isLoading
                                    ? Text("next".tr,
                                        style: sfBold.copyWith(
                                            fontSize: 18, color: Colors.white))
                                    : CircularProgressIndicator.adaptive(
                                        valueColor:
                                            AlwaysStoppedAnimation<Color>(
                                                Colors.white),
                                      ),
                              ),
                            ),
                          )
                        ]),
                  );
                }),
              ),
            ),
          ),
        )
      ]),
    ));
  }

  void _register(AuthController authController) async {
    authController
        .registerUser(_firstNameController.text, _referralController.text)
        .then((status) async {
      if (status.isSuccess) {
        Get.toNamed(RouteHelper.getAccessLocationRoute(RouteHelper.signUp));
      } else {
        showCustomSnackBar(status.message);
      }
    });
  }
}
